package com.jialanmo.helper.viewpager;

import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jialanmo.helper.R;

import java.util.List;

public final class SlidePagerAdapter<T> extends BasePagerAdapter<T> {

    private final BasePagerAdapter<T> mOriginalAdapter;

    private final View mRootView;
    private final LinearLayout mLinearLayoutTipGroup;
    private final ViewPager mViewPager;

    private final ViewPager.SimpleOnPageChangeListener mSimpleOnPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            for (int i = 0; i < mLinearLayoutTipGroup.getChildCount(); i++) {
                if (position == i) {
                    mLinearLayoutTipGroup.getChildAt(i).setBackgroundResource(R.drawable.ic_slideviewpager_tip_active);
                } else {
                    mLinearLayoutTipGroup.getChildAt(i).setBackgroundResource(R.drawable.ic_slideviewpager_tip_inactive);
                }
            }
        }
    };

    public SlidePagerAdapter(ViewGroup parent, BasePagerAdapter<T> adapter) {
        mRootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.slideviewpager, parent, false);
        mLinearLayoutTipGroup = (LinearLayout) mRootView.findViewById(R.id.slideviewpager_linearlayout_tips);
        mViewPager = (ViewPager) mRootView.findViewById(R.id.slideviewpager_viewpager);
        mViewPager.addOnPageChangeListener(mSimpleOnPageChangeListener);
        mOriginalAdapter = adapter;
        mViewPager.setAdapter(this);
    }

    @Override
    public List<T> getData() {
        return mOriginalAdapter.getData();
    }

    @Override
    public int getCount() {
        return mOriginalAdapter.getCount();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return mOriginalAdapter.isViewFromObject(view, object);
    }

    @Override
    public T instantiateItem(ViewGroup container, int position) {
        return mOriginalAdapter.instantiateItem(container, position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        mOriginalAdapter.destroyItem(container, position, object);
    }

    @Override
    public int getItemPosition(Object object) {
        return mOriginalAdapter.getItemPosition(object);
    }

    @Override
    public void resetData(List<T> data) {
        mOriginalAdapter.resetData(data);
        drawTips();
        notifyDataSetChanged();
        mViewPager.setCurrentItem(0);
        mSimpleOnPageChangeListener.onPageSelected(0);
    }

    public View getRootView() {
        return mRootView;
    }

    private void drawTips() {
        if (mLinearLayoutTipGroup != null && mLinearLayoutTipGroup.getChildCount() != getCount()) {
            if (mLinearLayoutTipGroup.getChildCount() > getCount()) {
                mLinearLayoutTipGroup.removeViews(0, mLinearLayoutTipGroup.getChildCount() - getCount());
            } else {
                int length = getCount() - mLinearLayoutTipGroup.getChildCount(),
                        tipsWidthAndHeight = (int) (10 * mLinearLayoutTipGroup.getContext().getResources().getDisplayMetrics().density);
                for (int i = 0; i < length; i++) {
                    ImageView imageView = new ImageView(mLinearLayoutTipGroup.getContext());
                    imageView.setBackgroundResource(R.drawable.ic_slideviewpager_tip_inactive);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(tipsWidthAndHeight, tipsWidthAndHeight);
                    layoutParams.leftMargin = layoutParams.rightMargin = 5;
                    mLinearLayoutTipGroup.addView(imageView, i, layoutParams);
                }
            }
        }
    }
}
